import {ApplicationParamService} from '@trident/shared';
import {Environment} from '../environments/environment';
import {KeycloakService} from 'keycloak-iop';
import {CookiesService} from 'ng-zorro-iop';
import {Injectable, Injector} from '@angular/core';
import {Router} from '@angular/router';

export function initializer(appConnfig: AppConfig, applicationParamService: ApplicationParamService): Function {
  return () => appConnfig.init(applicationParamService);
}

@Injectable()
export class AppConfig {
  private router: Router;

  constructor(private keycloak: KeycloakService, private cookieService: CookiesService, injector: Injector) {
    setTimeout(() => {
      this.router = injector.get(Router);

    });
  }

  public init(applicationParamService: ApplicationParamService) {
    return new Promise(async (resolve, reject) => {
      await applicationParamService.getParams().then(datas => {
        Environment.application = datas;
      });
      try {
        // await this.keycloak.init('token', {
        //   url: Environment.application.authUrl,
        //   realm: Environment.application.authRealm,
        //   clientId: Environment.application.authClientId
        // });
        resolve();
      } catch (error) {
        reject(error);
      }
    });
  }
}
